package com.lagou.edu.factory;

import com.alibaba.druid.util.StringUtils;
import com.lagou.edu.annotation.MyAutowired;
import com.lagou.edu.annotation.MyService;
import com.lagou.edu.annotation.MyTransactional;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.reflections.Reflections;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BeanFactory {
    public static Map<String, Object> map = new HashMap<>();

    static {
        try{
            Reflections edus = new Reflections("com.lagou.edu");
            Set<Class<?>> clazzs = edus.getTypesAnnotatedWith(MyService.class);
            //遍历对象集合
            for (Class<?> clazz:clazzs) {
                Object object = clazz.newInstance();
                MyService service = clazz.getAnnotation(MyService.class);
                if(StringUtils.isEmpty(service.value())){
                    String[] names = clazz.getName().split("\\.");
                    map.put(names[names.length-1], object);
                }else{
                    map.put(service.value(), object);
                }
            }
            for(Map.Entry<String, Object> entrySet: map.entrySet()) {
                Object obj = entrySet.getValue();
                Class clazz = obj.getClass();
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    if (field.isAnnotationPresent(MyAutowired.class)
                            && field.getAnnotation(MyAutowired.class).value()) {//有使用注解则注入
                        String[] names = field.getType().getName().split("\\.");
                        String name = names[names.length - 1];
                        Method[] methods = clazz.getMethods();
                        for (int j = 0; j < methods.length; j++) {
                            Method method = methods[j];
                            if (method.getName().equalsIgnoreCase("set" + name)) {  // 该方法就是 setAccountDao(AccountDao accountDao)
                                method.invoke(obj, map.get(name));
                            }
                        }
                    }
                }
                if(clazz.isAnnotationPresent(MyTransactional.class)){
                    ProxyFactory proxyFactory = (ProxyFactory) BeanFactory.getBean("proxyFactory");
                    Class[] face = clazz.getInterfaces();//获取类c实现的所有接口
                    if(face!=null&&face.length>0){
                        obj = proxyFactory.getJdkProxy(obj);
                    }else{
                        obj = proxyFactory.getCglibProxy(obj);
                    }
                }
                map.put(entrySet.getKey(),obj);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static Object getBean(String id) {
        return map.get(id);
    }
}
